<?php

namespace App\depository;

use App\Models\ManageSecretModel;

class ManageSecretDep
{
    public $model;

    CONST STATUS_ON = 1; // 已使用
    CONST STATUS_OFF = 2; // 未使用
    /**
     * @var
     * 状态
     */
    CONST STATUS = [
        self::STATUS_ON => '已使用',
        self::STATUS_OFF => '未使用',
    ];

    public function __construct(){
        $this->model = new ManageSecretModel();
    }


    public function firstBySecret($secret){

        $res = $this->model->where('secret', $secret)->first();
        return $res;
    }

    public function firstBySteamId($steamId,$status){

        $res = $this->model->where('steamid',$steamId)->where('status',$status)->first();
        return $res;

    }

    public function add($data)
    {
        $res = $this->model->insert($data);
        return $res;
    }

    public function edit($data)
    {
        $this->model->where('id', $data['id'])->update([
            'steamid' => $data['steamid'],
            'extra' => $data['extra'],
            'group_name' => $data['group_name']
        ]);
    }

    public function upd($id,$data){
        $res = $this->model->where('id', $id)->update($data);
        return $res;
    }

    public function get_fields($fields = [], $fields_key = '', $fields_val = '')
    {
        return $this->model->select($fields)->where($fields_key, $fields_val)->where("status", self::STATUS_ON)->first();
    }

    public function get_steam_info($steamid)
    {
        $res = $this->model
            ->where("steamid", $steamid)
            ->where("status", self::STATUS_ON)
            ->where("end_time", ">", date("Y-m-d H:i:s"))
            ->orderBy("id", "desc")
            ->first();
        return $res;
    }

    public function list($param)
    {
        $res = $this->model
            ->where('gh_id', $param['gh_id'])
            ->when(!empty($param['date']), function($query) use ($param) {
                $query->where("start_time", ">=", $param['date'][0]);
                $query->where("start_time", "<=", $param['date'][1]);
            })
            ->when(!empty($param['id']), function ($query) use ($param) {
                 $query->where("id",$param['id']);
            })
            ->when(!empty($param['secret']), function ($query) use ($param) {
                 $query->where("secret", $param['secret']);
            })
            ->when(!empty($param['status']), function ($query) use ($param) {
                 $query->where("status", $param['status']);
            })
            ->when(!empty($param['steamid']), function($query) use ($param){
                $query->where('steamid', $param['steamid']);
            })
            ->orderBy('id', 'desc')
            ->paginate($param['per_page'], ['*'], 'page', $param['current_page']);;
        return $res;
    }

    public function del($id)
    {
        $this->model->where('id', $id)->delete();
    }

    public function getByStatus($teamId,$status){
        $res = $this->model->where('status',$status)->where('gh_id',$teamId)->get();
        return $res;
    }
}
